#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include"RoundRobinQueue.h"


MyCircularQueue* myCircularQueueCreate(int k) {
    MyCircularQueue* newQueue = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    newQueue->data = (int*)malloc(sizeof(int) * (k + 1));
    newQueue->head = newQueue->tail = -1;
    newQueue->length = k + 1;
    newQueue->size = 0;

    return newQueue;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj)) {
        return false;
    }
    obj->head = (obj->head + 1) % (obj->length);
    --obj->size;
    return true;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    if (myCircularQueueIsFull(obj))
    {
        return false;
    }
    if (obj->head == -1)
    {
        obj->head = 0;
        obj->tail = 0;
    }
    else
    {
        obj->tail = (obj->tail + 1) % (obj->length);
    }
    obj->data[obj->tail] = value;
    ++obj->size;
    return true;
}

int myCircularQueueFront(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
    {
        return -1;
    }
    return obj->data[obj->head];
}

int myCircularQueueRear(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
    {
        return -1;
    }
    return obj->data[obj->tail];
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    return obj->size == 0;
}

bool myCircularQueueIsFull(MyCircularQueue* obj) {

    return obj->size == obj->length - 1;
}

void myCircularQueueFree(MyCircularQueue* obj) {
    free(obj->data);
    free(obj);
}